<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>脚本管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="6">
          项目:
          <el-input
            v-model="queryInfo.epic"
            placeholder="请输入项目"
            @input="searchTestscriptList()"
            clearable
            size="small"
          >
          </el-input>
        </el-col>
        <el-col :span="6">
          模块:
          <el-input
            v-model="queryInfo.feature"
            placeholder="请输入模块"
            @input="searchTestscriptList()"
            clearable
            size="small"
          ></el-input>
        </el-col>
        <el-col :span="6">
          子模块:
          <el-input
            v-model="queryInfo.story"
            placeholder="请输入子模块"
            @input="searchTestscriptList()"
            clearable
            size="small"
          ></el-input>
        </el-col>
        <el-col :span="6">
          测试标题:
          <el-input
            v-model="queryInfo.title"
            placeholder="请输入测试标题"
            @input="searchTestscriptList()"
            clearable
            size="small"
          ></el-input>
        </el-col>
      </el-row>
      <el-row :gutter="20" style="margin-top: 10px">
        <el-col :span="6">
          标签:
          <el-radio-group v-model="queryInfo.marks" @change="radioChange">
            <el-radio label="test"></el-radio>
            <el-radio label="pro"></el-radio>
            <el-radio label="core"></el-radio>
          </el-radio-group>
        </el-col>
        <el-col :span="6">
          状态:
          <el-select
            v-model="queryInfo.has_test_case"
            placeholder="请选择"
            size="small"
            clearable
            @change="selectChange"
          >
            <el-option
              v-for="item in status"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="6">
          维护人员:
          <el-input
            v-model="queryInfo.author"
            placeholder="请输入维护人员"
            @input="searchTestscriptList()"
            clearable
            size="small"
          ></el-input>
        </el-col>
      </el-row>
    </el-card>

    <el-card style="margin-top: 10px">
      <el-row>
        <el-col :span="2">
          <el-button type="primary" size="mini" @click="setBatchTaskName"
            >批量执行</el-button>
        </el-col>
      </el-row>
      <!-- 测试用例列表区域 -->
      <el-table
        :data="testscriptList"
        border
        stripe
        ref="multipleTable"
        @selection-change="handleSelectionChange"
        :row-key="getRowKeys"
      >
        <el-table-column
          label="#"
          type="selection"
          reserve-selection
        ></el-table-column>
        <el-table-column label="项目" prop="epic"></el-table-column>
        <el-table-column label="模块" prop="feature"></el-table-column>
        <el-table-column label="子模块" prop="story"></el-table-column>
        <el-table-column label="测试用例标题" prop="title"></el-table-column>
        <el-table-column
          label="测试用例编号"
          prop="test_case_id"
        ></el-table-column>
        <el-table-column label="标签" prop="marks"></el-table-column>
        <el-table-column label="状态" prop="has_test_case">
          <template slot-scope="scope">
            {{ scope.row.has_test_case ? "已关联" : "未关联" }}
          </template>
        </el-table-column>
        <el-table-column label="维护人员" prop="author"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="setTaskName(scope.row)"
              >执行</el-button
            >
            <el-divider direction="vertical"></el-divider>
            <el-button
              type="text"
              size="mini"
              @click="showDataSource(scope.row)"
              >查看脚本</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.page_num"
        :page-sizes="[10, 20, 40, 80]"
        :page-size="queryInfo.page_size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

      <el-dialog
        title="测试任务"
        :visible.sync="testtaskdialogVisible"
        width="30%"
      >
        <el-form
          ref="executeFormRef"
          :model="executeForm"
          :rules="executeRules"
          label-width="120px"
          label-suffix=":"
          :append-to-body="true"
        >
          <el-form-item label="任务名称" prop="task_name">
            <el-input v-model="executeForm.task_name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="testtaskdialogVisible = false" size="small"
            >取 消</el-button
          >
          <el-button type="primary" @click="handleExecute()" size="small"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 获取用户列表的参数对象
      queryInfo: {
        //   项目
        epic: "",
        // 模块
        feature: "",
        // 子模块
        story: "",
        // 测试标题
        title: "",
        // 用例编号
        test_case_id: "",
        // 是否存在测试脚本
        has_test_case: "",
        // 标记
        marks: "",
        // 当前的页数
        page_num: 1,
        // 当前每页显示多少条数据
        page_size: 10,
      },
      // 脚本列表数据
      testscriptList: [],
      // 脚本总数
      total: 0,
      // 列表多选数据
      multipleSelection: [],
      // 支持跨页多选
      getRowKeys(row) {
        // console.log(row)
        return row.id;
      },
      // 用例关联状态
      status: [
        {
          value: true,
          label: "已关联",
        },
        {
          value: false,
          label: "未关联",
        },
      ],
      testtaskdialogVisible: false,
      // 单次执行任务名称form
      executeForm: {
        task_name: "",
      },
      // 单次执行的验证规则
      executeRules: {
        task_name: [
          {
            required: true,
            message: "请输入任务名称",
            trigger: "blur",
          },
        ],
      },

      // 需要执行的脚本
      executeTestScript: "",
    };
  },
  created() {
    this.getTestscriptList();
  },
  methods: {
    // 获取测试用例数据
    async getTestscriptList() {
      const { data: res } = await this.$http.post(
        "/testscript/list",
        this.qs.stringify(this.queryInfo)
      );
      // 查询失败
      if (res.code !== 200) {
        return this.$message.error("获取用例数据失败");
      }
      this.testscriptList = res.data;
      this.total = res.count;
    },

    searchTestscriptList() {
      this.queryInfo.page_num = 1;
      this.getTestscriptList();
    },

    // 监听 pageSize 改变的事件
    handleSizeChange(newSize) {
      // console.log(newSize)
      this.queryInfo.page_num = 1;
      this.queryInfo.page_size = newSize;

      this.getTestscriptList();
    },

    // 监听页码值改变的事件
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.queryInfo.page_num = newPage;
      // this.queryInfo.page_size = 10;
      this.getTestscriptList();
    },

    // 监听标记值改变的事件
    radioChange(value) {
      this.queryInfo.page_num = 1;
      console.log(value);
      this.getTestscriptList();
    },

    // 监听状态值改变的事件
    selectChange(value) {
      this.queryInfo.page_num = 1;
      console.log(value);
      this.getTestscriptList();
    },

    // 监听多选列表数据变化
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(val);
    },
    // 查看脚本
    showDataSource: function(obj) {
      console.log(obj);
      //   const { href } = this.$router.resolve({
      //     name: "testScriptDataSource",
      //     query: { id: obj.id }
      //   });
      window.open(`/code.html?id=${obj.id}`, "_blank");
      //   this.$router.push({path:'/testScript/testScriptDataSource', query:{id: obj.id}})
    },

    // 设置单个脚本执行任务名称
    setTaskName: function(obj) {
      this.testtaskdialogVisible = true;
      this.executeForm.task_name = `${obj.title}-${new Date().getTime()}`;
      this.executeTestScript = obj.test_case_full_name;
      // console.log(this.task_name);
    },
    // 设置批量执行脚本任务名称
    setBatchTaskName: function() {
      if (this.multipleSelection.length == 0)
        return this.$message.error("请至少选择一个脚本!");
      this.testtaskdialogVisible = true;
      this.executeForm.task_name = `批量任务-${new Date().getTime()}`;
      var test_case_full_names = [];
      for (var i = 0; i < this.multipleSelection.length; i++) {
        test_case_full_names.push(
          // console.log(this.multipleSelection[i])
          this.multipleSelection[i].test_case_full_name
        );
      }
      this.executeTestScript = test_case_full_names.join(",");
      console.log(this.executeTestScript);
    },

    // 执行脚本
    async execute(task_name, testscripts) {
      let req = { testscripts: testscripts, task_name: task_name };
      const { data: res } = await this.$http.post(
        "/testscript/execute",
        this.qs.stringify(req)
      );
      if (res.code !== 200) {
        return this.$message.error(res.msg);
      }
      this.$message.success(res.msg);
    },

    // 点击按钮，执行脚本
    handleExecute() {
      this.$refs.executeFormRef.validate((valid) => {
        console.log(valid);
        if (!valid) return;
        //发起执行请求
        this.execute(this.executeForm.task_name, this.executeTestScript);
        this.testtaskdialogVisible = false;
        this.$refs.multipleTable.clearSelection();
      });
    },
  },
};
</script>

<style lang="less" scoped>
.el-input {
  width: auto;
}

.el-radio {
  margin-right: 15px;
}

/deep/.el-radio__label {
  padding-left: 5px !important;
}

/deep/.el-form-item__label {
  padding: 0;
  margin-right: 10px;
}
</style>
